<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select
          v-model="queryParams.userType"
          placeholder="请选择用户类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接收邮箱" prop="toMail">
        <el-input
          v-model="queryParams.toMail"
          placeholder="请输入接收邮箱"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="发送邮箱" prop="accountId">
        <el-select
          v-model="queryParams.accountId"
          placeholder="请选择邮箱账号"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="account in accountList"
            :key="account.id"
            :value="account.id"
            :label="account.mail"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模板编码" prop="templateCode">
        <el-input
          v-model="queryParams.templateCode"
          placeholder="请输入模板编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="发送状态" prop="sendStatus">
        <el-select
          v-model="queryParams.sendStatus"
          placeholder="请选择发送状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in system_mail_send_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发送时间" prop="sendTime">
        <el-date-picker
          v-model="queryParams.sendTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" prop="id" width="100" />
      <el-table-column label="接收邮箱" prop="toMail" width="210" />
      <el-table-column label="模板编码" prop="templateCode" width="210" />
      <el-table-column label="邮件标题" prop="templateTitle" />
      <el-table-column label="发送邮箱" prop="fromMail" width="200" />
      <el-table-column label="发送状态" prop="sendStatus" width="130">
        <template #default="scope">
          <dict-tag :options="system_mail_send_status" :value="scope.row.sendStatus" />
        </template>
      </el-table-column>
      <el-table-column label="发送时间" prop="sendTime" width="180px" />
      <el-table-column label="创建时间" prop="createTime" width="180px" />
      <el-table-column label="操作" width="80px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row)"
            v-hasPermi="['system:mail-log:query']"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：详情 -->
  <MailLogDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { MailLogApi, MailLogVO } from '@/api/system/mail/mail_log'
import { MailAccountApi, MailAccountVO } from '@/api/system/mail/mail_account'
import MailLogDetail from './MailLogDetail.vue'

/** 邮件日志 列表 */
defineOptions({ name: 'SystemMailLog' })

// const message = useMessage() // 消息弹窗
// const { t } = useI18n() // 国际化
const { common_user_type } = useDict(DICT_TYPE.common_user_type)
const { system_mail_send_status } = useIntDict(DICT_TYPE.system_mail_send_status)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<MailLogVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  userId: undefined,
  userType: undefined,
  toMail: undefined,
  accountId: undefined,
  templateCode: undefined,
  sendStatus: undefined,
  sendTime: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const accountList = ref<MailAccountVO[]>([]) // 邮箱账号列表

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await MailLogApi.getMailLogPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 详情操作 */
const detailRef = ref<InstanceType<typeof MailLogDetail>>()
const openDetail = (data: MailLogVO) => {
  detailRef.value?.open(data)
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  accountList.value = await MailAccountApi.getMailAccountSimpleList()
})
</script>

<style scoped lang="scss"></style>
